package top.itmeans.yes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import top.itmeans.yes.entity.HomeNewProduct;
import top.itmeans.yes.mapper.HomeNewProductMapper;
import top.itmeans.yes.service.HomeNewProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 新品推荐表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class HomeNewProductServiceImpl extends ServiceImpl<HomeNewProductMapper, HomeNewProduct> implements HomeNewProductService {

    @Resource
    private HomeNewProductMapper homeNewProductMapper;

    @Override
    public int create(List<HomeNewProduct> homeNewProductList) {
        for (HomeNewProduct homeNewProduct : homeNewProductList) {
            homeNewProduct.setRecommendStatus(1);
            homeNewProduct.setSort(0);
            homeNewProductMapper.insert(homeNewProduct);
        }
        return homeNewProductList.size();
    }

    @Override
    public int updateSort(Long id, Integer sort) {
        HomeNewProduct homeNewProduct = new HomeNewProduct();
        homeNewProduct.setId(id);
        homeNewProduct.setSort(sort);
        return homeNewProductMapper.updateById(homeNewProduct);
    }

    @Override
    public int delete(List<Long> ids) {
        return homeNewProductMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<HomeNewProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(HomeNewProduct::getId,ids);
        HomeNewProduct record = new HomeNewProduct();
        record.setRecommendStatus(recommendStatus);
        return homeNewProductMapper.update(record,updateWrapper);
    }

    @Override
    public Page<HomeNewProduct> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<HomeNewProduct> homeNewProductPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(productName)){
            queryWrapper.like(HomeNewProduct::getProductName,productName);
        }
        if(recommendStatus!=null){
            queryWrapper.like(HomeNewProduct::getRecommendStatus,recommendStatus);
        }
        queryWrapper.orderByDesc(HomeNewProduct::getSort);
        return homeNewProductMapper.selectPage(homeNewProductPage,queryWrapper);
    }
}
